

// Variation in construction of instrument




use "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv", clear 

cd  G:\Data\Workdata\707677\Common\03Output\02Regresssions



keep if time==1

tab obs_lawyer

egen lawyer_id_unique =group(lawyerid court) // Unique lawyer id within courts. some lawyers work in multiple courts
bys lawyer_id_unique: egen n_lawyer=count(lawyer_id_unique)
egen mean_lawyer_leniency= mean(granted), by(lawyer_id_unique)

gen iv_judge2_all = (mean_lawyer_leniency*n_lawyer - granted)/(n_lawyer-1)

gen iv_lawyer2=iv_judge2_all-(iv_court*obs_court-granted)/(obs_court-1)


// Vary by year 

egen lawyer_id_unique_year =group(lawyerid court pyear)

bys lawyer_id_unique_year: egen n_lawyer_year=count(lawyer_id_unique_year)
egen mean_lawyer_leniency_year= mean(granted), by(lawyer_id_unique pyear)

gen iv_lawyer_leave= (mean_lawyer_leniency_year*n_lawyer_year-granted)/(n_lawyer_year-1)

// Leave out year

gen iv_lawyer_vary=(mean_lawyer_leniency*n_lawyer-mean_lawyer_leniency_year*n_lawyer_year)/(n_lawyer-n_lawyer_year)

// Split sample

set seed 22

splitsample, generate(random_number) values(0 1)  // 0 to estimate leniency. 1 to estimate iv

egen lawyer_id_unique_split =group(lawyerid court random_number) // Unique lawyer id within courts. some lawyers work in multiple courts
bys lawyer_id_unique_split: egen n_lawyer_split=count(lawyer_id_unique_split)
egen mean_lawyer_leniency_split= mean(granted), by(lawyer_id_unique_split)



gen iv_lawyer_split=(mean_lawyer_leniency_split) if random_number==0


bys lawyer_id_unique: egen iv_lawyer_split2=mean(iv_lawyer_split)

keep if n_lawyer>=20

egen n_judge=nvals(lawyer_id_unique), by(court pyear)



tab n_judge

keep if n_judge>1

keep pnr iv_lawyer2 lawyer_id_unique iv_judge2_all iv_lawyer_leave iv_lawyer_vary iv_lawyer_split2 random_number




merge 1:m pnr using "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv"


keep if _merge==3
drop _merge

keep if time>0

tab lawyer_id_unique, gen(lawyer_dum)


egen court_pyear =group(court pyear) 
tab court_pyear

tab aar, gen(aardum)
tab pyear, gen(pyeardum)
tab court, gen(courtdum)


tab lawyer_id_unique, gen(trustee_dum)



local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"







// Standard


sum erhvervsindk_real if granted==0


local dismissed_mean=r(mean)


sum erhvervsindk_real

local dependent_mean=r(mean)



reg erhvervsindk_real granted `Xvar_short' i.court_pyear if time==1

local individuals=e(N)


ivreghdfe erhvervsindk_real (granted=iv_lawyer2) `Xvar_short' aardum2-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 

outreg2 using iv_vary_instrument_20_standard.xls, replace ctitle(standard IV erhvervsindk_real) ///
addstat(Non-granted mean, `dismissed_mean', dependent_mean, `dependent_mean', individuals, `individuals')



// Vary leniency construction by year

sum erhvervsindk_real if granted==0 & iv_lawyer_leave!=.


local dismissed_mean=r(mean)

sum erhvervsindk_real if iv_lawyer_leave!=.

local dependent_mean=r(mean)


reg erhvervsindk_real granted `Xvar_short' i.court_pyear if time==1 & iv_lawyer_leave!=.


local individuals=e(N)


ivreghdfe erhvervsindk_real (granted=iv_lawyer_leave) `Xvar_short' aardum2-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_vary_instrument_20_standard.xls, append ctitle(IV erhvervsindk_real vary by year) ///
addstat(Non-granted mean, `dismissed_mean', dependent_mean, `dependent_mean', individuals, `individuals')




// Leave year out 

sum erhvervsindk_real if granted==0 & iv_lawyer_vary!=.
local dismissed_mean=r(mean)

sum erhvervsindk_real if iv_lawyer_vary!=.

local dependent_mean=r(mean)



reg erhvervsindk_real granted `Xvar_short' i.court_pyear if time==1 & iv_lawyer_vary!=.
local individuals=e(N)

ivreghdfe erhvervsindk_real (granted=iv_lawyer_vary) `Xvar_short' aardum2-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 



outreg2 using iv_vary_instrument_20_standard.xls, append ctitle(IV erhvervsindk_real leave year put) ///
addstat(Non-granted mean, `dismissed_mean', dependent_mean, `dependent_mean', individuals, `individuals')



// Splitsample

sum erhvervsindk_real if granted==0 & iv_lawyer_split2!=. & random_number==1
local dismissed_mean=r(mean)


sum erhvervsindk_real if iv_lawyer_split2!=. & random_number==1
local dependent_mean=r(mean)



reg erhvervsindk_real granted `Xvar_short' i.court_pyear if time==1 & iv_lawyer_split2!=. & random_number==1

local individuals=e(N)



ivreghdfe erhvervsindk_real (granted=iv_lawyer_split2) `Xvar_short' aardum2-aardum35 if random_number==1, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_vary_instrument_20_standard.xls, append ctitle(IV erhvervsindk_real split sample) ///
addstat(Non-granted mean, `dismissed_mean', dependent_mean, `dependent_mean', individuals, `individuals')




local depvar "employed_pstill unemployed_pstill out_of_lf_pstill timelon_real aktiv_real passiv_real formue_real koejd_real house tilbtot_real disability kontant_real social attrition mortgage_real"






foreach var of varlist `depvar'{


// Standard



sum `var' if granted==0
local dismissed_mean=r(mean)


sum `var'
local dependent_mean=r(mean)



reg `var' granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)

ivreghdfe `var' (granted=iv_lawyer2) `Xvar_short' aardum2-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 

outreg2 using iv_vary_instrument_20_standard.xls, append ctitle(standard IV `var') ///
 addstat(Non-granted mean, `dismissed_mean', dependent_mean, `dependent_mean', individuals, `individuals')



// Vary leniency construction by year

sum `var' if granted==0 & iv_lawyer_leave!=.
local dismissed_mean=r(mean)


sum `var' if iv_lawyer_leave!=.
local dependent_mean=r(mean)



reg `var' granted `Xvar_short' i.court_pyear if time==1 & iv_lawyer_leave!=.
local individuals=e(N)


ivreghdfe `var' (granted=iv_lawyer_leave) `Xvar_short' aardum2-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 

outreg2 using iv_vary_instrument_20_standard.xls, append ctitle(IV `var' vary by year) ///
 addstat(Non-granted mean, `dismissed_mean', dependent_mean, `dependent_mean', individuals, `individuals')




// Leave year out 

sum `var' if granted==0 & iv_lawyer_vary!=.
local dismissed_mean=r(mean)


sum `var' if iv_lawyer_vary!=.
local dependent_mean=r(mean)



reg `var' granted `Xvar_short' i.court_pyear if time==1 & iv_lawyer_vary!=.
local individuals=e(N)


ivreghdfe `var' (granted=iv_lawyer_vary) `Xvar_short' aardum2-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 

outreg2 using iv_vary_instrument_20_standard.xls, append ctitle(IV `var' leave year put) ///
 addstat(Non-granted mean, `dismissed_mean', dependent_mean, `dependent_mean', individuals, `individuals')



// Splitsample


sum `var' if granted==0 & iv_lawyer_split2!=. & random_number==1
local dismissed_mean=r(mean)


sum `var' if iv_lawyer_split2!=. & random_number==1
local dependent_mean=r(mean)



reg `var' granted `Xvar_short' i.court_pyear if time==1 & iv_lawyer_split2!=. & random_number==1
local individuals=e(N)

ivreghdfe `var' (granted=iv_lawyer_split2) `Xvar_short' aardum2-aardum35 if random_number==1, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 

outreg2 using iv_vary_instrument_20_standard.xls, append ctitle(IV `var' split sample) ///
 addstat(Non-granted mean, `dismissed_mean', dependent_mean, `dependent_mean', individuals, `individuals')

}






// Standard



sum bankgaeld_real if granted==0
local dismissed_mean=r(mean)


sum bankgaeld_real
local dependent_mean=r(mean)



reg bankgaeld_real granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)




ivregress 2sls bankgaeld_real (granted=iv_lawyer2) `Xvar_short' aardum2-aardum35, cluster(lawyer_id_unique) 


outreg2 using iv_vary_instrument_20_standard.xls, append ctitle(standard IV bankgaeld_real) ///
 addstat(Non-granted mean, `dismissed_mean', dependent_mean, `dependent_mean', individuals, `individuals')



// Vary leniency construction by year

sum bankgaeld_real if granted==0 & iv_lawyer_leave!=.
local dismissed_mean=r(mean)


sum bankgaeld_real if iv_lawyer_leave!=.
local dependent_mean=r(mean)



reg bankgaeld_real granted `Xvar_short' i.court_pyear if time==1 & iv_lawyer_leave!=.
local individuals=e(N)



ivregress 2sls bankgaeld_real (granted=iv_lawyer_leave) `Xvar_short' aardum2-aardum35, cluster(lawyer_id_unique) 

outreg2 using iv_vary_instrument_20_standard.xls, append ctitle(IV bankgaeld_real vary by year) ///
 addstat(Non-granted mean, `dismissed_mean', dependent_mean, `dependent_mean', individuals, `individuals')




// Leave year out 

sum bankgaeld_real if granted==0 & iv_lawyer_vary!=.
local dismissed_mean=r(mean)


sum bankgaeld_real if iv_lawyer_vary!=.
local dependent_mean=r(mean)



reg bankgaeld_real granted `Xvar_short' i.court_pyear if time==1 & iv_lawyer_vary!=.
local individuals=e(N)




ivregress 2sls bankgaeld_real (granted=iv_lawyer_vary) `Xvar_short' aardum2-aardum35, cluster(lawyer_id_unique) 

outreg2 using iv_vary_instrument_20_standard.xls, append ctitle(IV bankgaeld_real leave year put) ///
 addstat(Non-granted mean, `dismissed_mean', dependent_mean, `dependent_mean', individuals, `individuals')



// Splitsample


sum bankgaeld_real if granted==0 & iv_lawyer_split2!=. & random_number==1
local dismissed_mean=r(mean)


sum bankgaeld_real if iv_lawyer_split2!=. & random_number==1
local dependent_mean=r(mean)



reg bankgaeld_real granted `Xvar_short' i.court_pyear if time==1 & iv_lawyer_split2!=. & random_number==1
local individuals=e(N)



ivregress 2sls bankgaeld_real (granted=iv_lawyer_split2) `Xvar_short' aardum2-aardum35, cluster(lawyer_id_unique) 


outreg2 using iv_vary_instrument_20_standard.xls, append ctitle(IV bankgaeld_real split sample) ///
 addstat(Non-granted mean, `dismissed_mean', dependent_mean, `dependent_mean', individuals, `individuals')



